/**
 * THIS OBJECT IS MEANT TO BE THE 'SERVER SIDE' OF THE CLIENT
 * IN OTHER WORDS, IT IS SUPPOSED TO RECEIVE INVOCATIONS FROM THE RMI SERVER
 * So it behaves as a REMOTE OBSERVER
 * TO BE COMPLETED BY THE STUDENTS
 */


package RMIClient;

import util.observer.rmi.RemoteObserver;

import java.rmi.RemoteException;

import eventReservationServer.EventRMIDTO;
import eventReservationServer.IReservationEventServer;

public class EventRemoteObserver extends RemoteObserver
{
	private static final long serialVersionUID = 1L;

	/**
	 * Creates a new instance of RemoteObserver ADD AS MUCH INFORMATION AS
	 * NEEDED
	 */
	
	private IReservationEventServer server = null;
	private EventController eventController = null;

	public EventRemoteObserver(IReservationEventServer server, EventController eventController) throws RemoteException
	{
		super();
		
		this.server = server;
		this.eventController = eventController;
		
		// Notifico al servidor de que hay un nuevo EventRemoteObserver
		server.addRemoteObserver(this);
	}

	public void stop()
	{
		try
		{
			// Notifico al servidor de que elimine un observador
			server.deleteRemoteObserver(this);
		} 
		catch (RemoteException e)
		{
			 System.err.println("Stop exception: " + e.getMessage());
			 e.printStackTrace();
		}
	}

	public void update(Object arg) throws java.rmi.RemoteException
	{
		// La actualizacion del evento que he recibido se la envio al controller para que la propague entre todos los observers
		if (arg instanceof EventRMIDTO)
		{
			eventController.updateEvent((EventRMIDTO) arg);
		}
	}
}
